package com.winhxd.b2c.common.feign.search;

import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.constant.ServiceName;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.product.vo.ProductVO;
import com.winhxd.b2c.common.domain.search.dto.SkuChanged;
import com.winhxd.b2c.common.domain.search.model.ProductDocument;
import com.winhxd.b2c.common.domain.search.dto.ProductSearchParam;
import com.winhxd.b2c.common.domain.search.dto.SearchResult;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * 商品搜索
 */
@FeignClient(value = ServiceName.SEARCH_SERVICE, fallbackFactory = ProductSearchClientFallbackFactory.class)
public interface ProductSearchClient {
    /**
     * 查询商品数据
     *
     * @param searchParam
     * @return
     */
    @RequestMapping(value = "/search/9001/v1/product/search", method = RequestMethod.POST)
    ResponseResult<SearchResult<ProductDocument>> search(@RequestBody ProductSearchParam searchParam);

    /**
     * 导入商品数据
     *
     * @param productVOList
     * @return
     */
    @RequestMapping(value = "/search/9002/v1/product/import", method = RequestMethod.POST)
    ResponseResult<Boolean> importProduct(@RequestBody List<ProductVO> productVOList);

    /**
     * 删除商品Spu数据
     *
     * @param spuIds
     * @return
     */
    @RequestMapping(value = "/search/9003/v1/product/deleteSpuList", method = RequestMethod.POST)
    ResponseResult<Boolean> deleteSpuList(@RequestBody List<Long> spuIds);

    /**
     * 更新sku库存及销量数据
     *
     * @param changes
     * @return
     */
    @RequestMapping(value = "/search/9004/v1/product/updateSku", method = RequestMethod.POST)
    ResponseResult<Boolean> updateSku(@RequestBody List<SkuChanged> changes);
}

@Component
class ProductSearchClientFallbackFactory implements FallbackFactory<ProductSearchClient> {
    private static final Logger logger = LoggerFactory.getLogger(ProductSearchClientFallbackFactory.class);

    @Override
    public ProductSearchClient create(Throwable throwable) {
        return new ProductSearchClient() {
            @Override
            public ResponseResult<SearchResult<ProductDocument>> search(ProductSearchParam searchParam) {
                logger.error("ProductSearchClient -> search", throwable);
                return new ResponseResult<>(BusinessCode.CODE_1001);
            }

            @Override
            public ResponseResult<Boolean> importProduct(List<ProductVO> productVOList) {
                logger.error("ProductSearchClient -> importProduct", throwable);
                return new ResponseResult<>(BusinessCode.CODE_1001);
            }

            @Override
            public ResponseResult<Boolean> deleteSpuList(List<Long> spuIds) {
                logger.error("ProductSearchClient -> deleteSpuList", throwable);
                return new ResponseResult<>(BusinessCode.CODE_1001);
            }

            @Override
            public ResponseResult<Boolean> updateSku(List<SkuChanged> changes) {
                logger.error("ProductSearchClient -> updateSku", throwable);
                return new ResponseResult<>(BusinessCode.CODE_1001);
            }
        };
    }
}
